package com.woniu.guoguo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.guoguo.common.Result;
import com.woniu.guoguo.model.Menu;
import com.woniu.guoguo.model.MenuImg;
import com.woniu.guoguo.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Richie
 * @since 2020-11-02
 */
@RestController
@CrossOrigin("*")
public class MenuController {
    @Autowired
    private IMenuService menuService;
    @RequestMapping("getmenu")
    public Result getmenu(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int size,
                          String type){
        Page<Menu> page=new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        IPage<Menu> iPage=menuService.getmenubytype(page,type);
        return Result.success(iPage);
    }
    @RequestMapping("getmenuimg")
    public Result getmenuimg(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int size,
                             String type){
      Page<String> page=new Page<>();
      page.setCurrent(currPage);
      page.setSize(size);
      IPage<String> iPage=menuService.getmenuimgbytype(page,type);
      return  Result.success(iPage);
    }
    @RequestMapping("getmenubyxl")
    public Result getmenubyxl(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int size,
                              String type){
        Page<String> page=new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        IPage<Menu> iPage=menuService.xlgetmenu(page,type);
        return Result.success(iPage);
    }
    @RequestMapping("getmenuimgbyxl")
    public Result getmenuimgbyxl(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int size,
                                 String type){
        Page<String> page=new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        IPage<String> iPage=menuService.xlgetmenuimg(page,type);
        return  Result.success(iPage);
    }
    @RequestMapping("getmenubyjg")
    public Result getmenubyjg(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int size,
                              String type){
        Page<String> page=new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        IPage<Menu> iPage=menuService.jggetmenu(page,type);
        return Result.success(iPage);
    }
    @RequestMapping("getmenuimgbyjg")
    public Result getmenuimgbyjg(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int size,
                                 String type){
        Page<String> page=new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        IPage<String> iPage=menuService.jggetmenuimg(page,type);
        return Result.success(iPage);
    }

    @GetMapping("/getMenuId")
    public Result getMenuId(HttpSession session) {
        if (session.getAttribute("menuId") != null && !(session.getAttribute("menuId") + "").equals("")) {
            String menuId = (Integer) session.getAttribute("menuId") + "";
            System.out.println(menuId);
            Map<String, String> menu = new HashMap<>();
            menu.put("menuId", menuId);
            return Result.success(menu);
        } else {
            System.out.println("没有套餐ID");
            return Result.fail("没有套餐ID");
        }
    }



    //根据名字
    @RequestMapping("getmenu2")
    public Result getmenu2(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int size,
                           String name){
        Page<Menu> page=new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        IPage<Menu> iPage=menuService.getmenubyname(page,name);
        return Result.success(iPage);
    }
    @RequestMapping("getmenuimg2")
    public Result getmenuimg2(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int size,
                              String name){
        Page<String> page=new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        IPage<String> iPage=menuService.getmenuimgbyname(page,name);
        return  Result.success(iPage);
    }
    @RequestMapping("getmenubyxl2")
    public Result getmenubyxl2(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int size,
                               String name){
        Page<String> page=new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        IPage<Menu> iPage=menuService.xlgetmenu2(page,name);
        return Result.success(iPage);
    }
    @RequestMapping("getmenuimgbyxl2")
    public Result getmenuimgbyxl2(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int size,
                                  String name){
        Page<String> page=new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        IPage<String> iPage=menuService.xlgetmenuimg2(page,name);
        return  Result.success(iPage);
    }
    @RequestMapping("getmenubyjg2")
    public Result getmenubyjg2(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int size,
                               String name){
        Page<String> page=new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        IPage<Menu> iPage=menuService.jggetmenu2(page,name);
        return Result.success(iPage);
    }
    @RequestMapping("getmenuimgbyjg2")
    public Result getmenuimgbyjg2(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int size,
                                  String name){
        Page<String> page=new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        IPage<String> iPage=menuService.jggetmenuimg2(page,name);
        return Result.success(iPage);
    }

    @GetMapping("/getSearchWord")
    public Result getSearchWord(HttpSession session) {
        if (session.getAttribute("searchWord") != null && ((String) session.getAttribute("searchWord")) != "") {
            return Result.success((String) session.getAttribute("searchWord"));
        } else {
            return Result.fail("没有搜索关键字");
        }
    }


    @GetMapping("/getType")
    public Result getType(HttpSession session) {
        if (session.getAttribute("menuType") != null && ((String) session.getAttribute("menuType")) != "") {
            return Result.success((String) session.getAttribute("menuType"));
        } else {
            return Result.fail("没有套餐类型");
        }
    }


}

